/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODECPP_UTIL_APPLICATION_H_
#define NODECPP_UTIL_APPLICATION_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <ctime>
#include <string>
#include <NodeCpp/Platform.h>
#include <NodeCpp/Macros.h>
#include <NodeCpp/AtExit.h>
#include <NodeCpp/Util/OptionSet.h>
#include <NodeCpp/Configuration.h>
#if defined(PLATFORM_WINDOWS)
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif // defined(PLATFORM_WINDOWS)
using NodeCpp::Configuration;

namespace NodeCpp {
namespace Util {

    class Application
    {
    public:
        enum ExitCode
        {
            EXIT_OK = 0,
            EXIT_USAGE = 64,
            EXIT_CONFIG = 65,
            EXIT_MEMORY = 66,
            EXIT_SUBSYSTEM = 67,
            EXIT_LOG = 68,
            EXIT_SOFTWARE = 70,
        };

        Application(const char* _AppName = "Application");
        virtual ~Application(void);

        int Run(int _Argc, char* _Argv[]);
        bool HookSignal(int _Signal, bool _Ignore = false);
        void UnhookSignal(int _Signal);

        time_t GetUpTime(void) const { return UpTime_; }

#if defined(PLATFORM_WINDOWS)
        static LONG WINAPI UnhandledExceptionFilter(struct _EXCEPTION_POINTERS*);
#endif // defined(PLATFORM_WINDOWS)
        static Application* Create(void);
        static Application* Instance(void);

		virtual const Configuration& conf(void) const = 0;
		static const struct Configuration::Any& conf(const std::string& _Key) { return Instance()->conf()[_Key]; }
    protected:
        virtual void OnDefineOptions(OptionSet& _Options);
        virtual int OnOption(const std::string& _Name, const std::string& _Arg);
        virtual int OnInitialize(void) = 0;
        virtual void OnSignal(int _Signal);
        virtual int Main(std::vector<std::string>& _UnknownArgs) = 0;
        virtual void OnUninitialize(void) = 0;

    private:
        int _Initialize(int _Argc, char* _Argv[]);
        void _Uninitialize(void);
        void _ShowUsage(void);
        static void _SignalHandler(int _Signal);

        const char* Name_;
        std::vector<std::string> Arguemnts_;
        Util::OptionSet Options_;
        time_t UpTime_;
        AtExit AtExit_;
        static Application* SingletonInst_;
        DISALLOW_COPY_AND_ASSIGN(Application);
    };

}}

#define IMPLEMENT_APPLICATION(BaseApp, UserApp)     \
    BaseApp* BaseApp::Create(void) {                \
        return new UserApp;                         \
    }                                               \

#define APPLICATION_CONF(Name)                                      \
    NodeCpp::Util::Application::Instance()->conf(Name)              \



#endif // NODECPP_UTIL_APPLICATION_H_
